//
//  MYGIndInfoTableViewCell.swift
//  MYDoctor
//
//  Created by 王振明 on 2024/4/4.
//

import UIKit

class MYGIndInfoTableViewCell: UITableViewCell {
    var modelIn:MYGIndInfoModel = MYGIndInfoModel(){
        didSet{
            if modelIn.is_hot == 1{
                hotImageV.isHidden = false
                nameLabel.snp.updateConstraints { make in
                    make.left.equalTo(hotImageV.snp.right).offset(SCALEWIDTH(8))
                }
            }else{
                hotImageV.isHidden = true
                nameLabel.snp.updateConstraints { make in
                    make.left.equalTo(hotImageV.snp.right).offset(SCALEWIDTH(-15))
                }
            }
            zanBtn.isSelected = modelIn.is_like == 1
            zanLabel.text = "\(modelIn.like_count)"
            commitLabel.text =  "\(modelIn.comment_count)"
            headerImageBV.sd_setImage(with: URL(string: modelIn.img))
            nameLabel.text = modelIn.title
            contentLab.text = modelIn.desc
            timeLabel.text = modelIn.created_at
            numberLabel.text = "\(String(format: "%d",modelIn.share_count))人分享了该资讯"
            numberLabel.rz.colorfulConfer {[weak self] confer in
                confer.text("\(String(format: "%d", self?.modelIn.share_count ?? 0))")?.font(.medium(14)).textColor(.rgba(99, 137, 241, 1))
                confer.text("人分享了该资讯")?.font(.regular(14)).textColor(.rgba(169, 169, 169, 1))
            }
        }
    }
    
    @objc func tapLike() {
        var likeStatus = "1"
        if self.modelIn.is_like == 0 {
            likeStatus = "1"
            self.zanBtn.isSelected = true
            self.modelIn.like_count = max(0, (self.modelIn.like_count + 1))
            self.modelIn.is_like = 1

        }else {
            likeStatus = "0"
            self.modelIn.like_count = max(0, (self.modelIn.like_count - 1))
            self.modelIn.is_like = 0
            self.zanBtn.isSelected = false
        }
        
        MYGNetwork<MYGMeAPI, MYGEmptyModel>.request(target: .likeSubmit(param: [
            "id":"\(self.modelIn.id)",
           "like_type":"6",
            "like_status":likeStatus])) { item in
               
       } failed: { _, error in
           MMYGHUD.showToast(text: error)
       }
    }
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        initUI()
    }
    private func initUI(){
        selectionStyle = .none
        contentView.addSubview(layerView)
        layerView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(SCALEWIDTH(15))
            make.top.equalToSuperview().offset(SCALEWIDTH(7.5))
            make.right.equalToSuperview().offset(-SCALEWIDTH(15))
            make.bottom.equalToSuperview().offset(-SCALEWIDTH(7.5))
        }

        layerView.addSubview(backView)
        backView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        backView.addSubview(hotImageV)
        hotImageV.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(SCALEWIDTH(10.5))
            make.top.equalToSuperview().offset(SCALEWIDTH(17))
            make.size.equalTo(CGSize(width: SCALEWIDTH(15), height: SCALEWIDTH(15)))
        }
        
        backView.addSubview(headerImageBV)
        headerImageBV.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-SCALEWIDTH(12.5))
            make.top.equalToSuperview().offset(SCALEWIDTH(15))
            make.size.equalTo(CGSize(width: SCALEWIDTH(104), height: SCALEWIDTH(83)))
        }
        
        backView.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(hotImageV.snp.right).offset(SCALEWIDTH(8))
            make.centerY.equalTo(hotImageV)
            make.height.equalTo(SCALEWIDTH(24))
            make.right.equalTo(headerImageBV.snp.left).offset(-SCALEWIDTH(10))
        }
        
        backView.addSubview(contentLab)
        contentLab.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(SCALEWIDTH(10.5))
            make.top.equalTo(nameLabel.snp.bottom).offset(SCALEWIDTH(4))
            make.height.equalTo(SCALEWIDTH(40))
            make.right.equalTo(headerImageBV.snp.left).offset(-SCALEWIDTH(10))
        }
        
        backView.addSubview(timeLabel)
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(contentLab.snp.left)
            make.top.equalTo(contentLab.snp.bottom).offset(SCALEWIDTH(10.5))
            make.height.equalTo(SCALEWIDTH(20))
        }
        
        
        backView.addSubview(zanLabel)
        zanLabel.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-SCALEWIDTH(8))
            make.top.equalTo(timeLabel.snp.bottom).offset(SCALEWIDTH(18))
            make.height.equalTo(SCALEWIDTH(20))
        }
        
        backView.addSubview(zanBtn)
        zanBtn.snp.makeConstraints { make in
            make.right.equalTo(zanLabel.snp.left).offset(-SCALEWIDTH(4))
            make.centerY.equalTo(zanLabel)
            make.size.equalTo(CGSize(width: SCALEWIDTH(14), height: SCALEWIDTH(13.5)))
        }
        
        backView.addSubview(commitLabel)
        commitLabel.snp.makeConstraints { make in
            make.right.equalTo(zanBtn.snp.left).offset(-SCALEWIDTH(10))
            make.centerY.equalTo(zanLabel)
            make.height.equalTo(SCALEWIDTH(20))
        }
        
        backView.addSubview(commentBtn)
        commentBtn.snp.makeConstraints { make in
            make.right.equalTo(commitLabel.snp.left).offset(-SCALEWIDTH(4))
            make.centerY.equalTo(zanLabel)
            make.size.equalTo(CGSize(width: SCALEWIDTH(14), height: SCALEWIDTH(13.5)))
            make.bottom.equalToSuperview().offset(-SCALEWIDTH(10))
        }
        
        backView.addSubview(shareImageV)
        shareImageV.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(SCALEWIDTH(10))
            make.top.equalTo(timeLabel.snp.bottom).offset(SCALEWIDTH(20))
            make.width.height.equalTo(SCALEWIDTH(15))
        }
        
        backView.addSubview(numberLabel)
        numberLabel.snp.makeConstraints { make in
            make.left.equalTo(shareImageV.snp.right).offset(SCALEWIDTH(5))
            make.centerY.equalTo(shareImageV)
            make.height.equalTo(SCALEWIDTH(20))
        }
        backView.addSubview(line)
        line.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(0.25)
            make.bottom.equalTo(zanBtn.snp.top).offset(-16)
        }
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private lazy var layerView: UIView = {
        let cont = UIView()
        cont.backgroundColor = .white
        cont.layer.shadowColor = RGBA(r: 200, g: 200, b: 200, a: 0.5).cgColor
        cont.layer.shadowOffset = CGSize(width: 2, height: 2)
        cont.layer.shadowRadius = 4.5
        cont.layer.shadowOpacity = 1
        cont.layer.cornerRadius = 10
        return cont
    }()
    
    private lazy var backView: UIView = {
        let cont = UIView()
        cont.layer.masksToBounds = true
        cont.layer.cornerRadius = SCALEWIDTH(10)
        return cont
    }()
    lazy var headerImageBV: UIImageView = {
        let cont = UIImageView()
        cont.layer.cornerRadius = SCALEWIDTH(10)
        cont.layer.masksToBounds = true
        cont.contentMode = .scaleAspectFill
        return cont
    }()
    lazy var nameLabel: UILabel = {
        let cont = UILabel()
        cont.text = "院士进藏助力神经外科医"
        cont.font = .medium(17)
        cont.textColor = .rgba(70, 70, 70)
        return cont
    }()
    private lazy var contentLab: UILabel = {
        let cont = UILabel()
        cont.text = "10日，中国科学院院士、国家神经系统疾病临床医学研究中医…."
        cont.font = .regular(14)
        cont.textColor = .rgba(152, 152, 152)
        cont.numberOfLines = 2
        return cont
    }()
    private lazy var timeLabel: UILabel = {
        let cont = UILabel()
        cont.text = "2023-01-01"
        cont.font = .regular(14)
        cont.textColor = .rgba(152, 152, 152)
        return cont
    }()
    private lazy var commentBtn: UIButton = {
        let cont = UIButton()
        cont.setImage(UIImage(named: "MYG_home_comment"), for: .normal)
        return cont
    }()
    private lazy var zanBtn: UIButton = {
        let cont = UIButton()
        cont.setImage(UIImage(named: "MYG_home_zan"), for: .normal)
        cont.setImage(UIImage(named: "MYG_cloudDisk_liked"), for: .selected)
        cont.setImage(UIImage(named: "MYG_cloudDisk_liked"), for: .highlighted)
        cont.addTarget(self, action: #selector(tapLike), for: .touchUpInside)
        return cont
    }()
    private lazy var zanLabel: UILabel = {
        let cont = UILabel()
        cont.text = "259"
        cont.font = .regular(12)
        cont.textColor = .rgba(128, 128, 128)
        return cont
    }()
    private lazy var commitLabel: UILabel = {
        let cont = UILabel()
        cont.text = "259"
        cont.font = .regular(12)
        cont.textColor = .rgba(128, 128, 128)
        return cont
    }()
    private lazy var numberLabel: UILabel = {
        let cont = UILabel()
        cont.text = "2592 人分享了该资讯"
        cont.font = .regular(12)
        cont.textColor = .rgba(128, 128, 128)
        return cont
    }()
    private lazy var shareImageV: UIImageView = {
        let cont = UIImageView()
        cont.image = UIImage.init(named: "MYG_cloudDisk_like_share")
        return cont
    }()
    lazy var hotImageV: UIImageView = {
        let cont = UIImageView()
        cont.image = UIImage(named: "MYG_home_hot")
        return cont
    }()
    
    var line:UIView = {
        let control = UIView()
        control.backgroundColor = UIColor.init(hexString: "#979797").withAlphaComponent(0.25)
        return control
    }()
}
